//
//  LOLFreeHeroViewController.m
//  
//
//  Created by mac on 16/1/28.
//
//

#import "LOLFreeHeroViewController.h"
#import "LOLBaseCollectionView.h"
#import "LOLFreeheroCell.h"
#import "LSZNetWorkTools.h"
#import "LOLHeroModel.h"
#import <UIImageView+WebCache.h>
#import "LOLHerodetaiViewController.h"
#define WIDTH [[UIScreen mainScreen]bounds].size.width
#define PADDING10 WIDTH/37.5
#define HEIGHT [[UIScreen mainScreen]bounds].size.height
#define TOPCHOOSEHEIGHT 30
@interface LOLFreeHeroViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) LOLBaseCollectionView *collectionView;


@end

@implementation LOLFreeHeroViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 15)];
    label.text = @"*每周五更新周免";
    label.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:label];
    
    self.dataSource = [NSMutableArray array];
    [self creatCollectionView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
          [self getdataFreedata];
    });
  
}

- (void)getdataFreedata {
     NSString *string = @"http://lolbox.duowan.com/phone/apiHeroes.php?&target=free";
    [LSZNetWorkTools getWithUrl:string parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"free"];
        for (NSDictionary *freeDic in array) {
            LOLHeroModel *model = [[LOLHeroModel alloc] initWithData:freeDic];
            [self.dataSource addObject:model];
        }
        [self.collectionView reloadData];
        
    
    } fail:^(NSError *error) {
        
        
    }];
}

- (void)creatCollectionView {
    self.collectionView = [[LOLBaseCollectionView alloc] initWithFrame:CGRectMake(10 ,15, WIDTH, HEIGHT-TOPCHOOSEHEIGHT-64 - 88) flowLayoutItemSize:CGSizeMake(WIDTH / 2 - PADDING10, (HEIGHT - 44 ) / 6) xSpace:0 ySpace:0               sectionInSet:UIEdgeInsetsMake(0, WIDTH /53.5, WIDTH / 53.5, WIDTH / 53.5) cell:[LOLFreeheroCell class] reuse:@"freeReuse"];
    _collectionView.contentInset = UIEdgeInsetsMake(WIDTH / 25, 0, 0, 0);
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LOLFreeheroCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"freeReuse" forIndexPath:indexPath];
    cell.model = _dataSource[indexPath.item];
    return cell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
      return _dataSource.count;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LOLHerodetaiViewController *heroDetailVC = [[LOLHerodetaiViewController alloc]init];
    LOLHeroModel *model = self.dataSource[indexPath.row];
    heroDetailVC.heroModel = model;
    [self.navigationController pushViewController:heroDetailVC animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
